
import java.io.InputStream;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class HibernateUtil 
{
    private static final SessionFactory sessionFactory;
    private static AnnotationConfiguration configuration = null;
    
    static 
    {
        configuration = new AnnotationConfiguration();      
       
        try 
        {
            InputStream r = HibernateUtil.class.getResourceAsStream("/hibernate.cfg.xml");
            System.out.println("r == " + r);
            if (null != r)
            {
                r.close();
            }
            
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = configuration.configure().buildSessionFactory();

         } 
         catch (Throwable ex) 
         {
                // Make sure you log the exception, as it might be swallowed
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
         }
    }
    
    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }
    
    public static void createSchema()
    {                  
        SchemaExport export = new SchemaExport(configuration);
        export.drop(false, true);
        export.create(false, true);
    }
}